<!-- 新建根目录 -->
<template>
  <div>
    <a-modal
      :title="title"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <a-input placeholder="请输入目录名" ref="inputRef" :maxLength="50" v-model="model.source" size="large" @change="handleInputChange" />
    </a-modal>
  </div>
</template>
<script>
import { postAction } from '@/api/manage'
export default {
  name: 'CreateDirectory',
  data() {
    return {
      title: '请输入目录名',
      action: '', // 类型
      target: '', // 上一级目录的id值
      visible: false,
      confirmLoading: false,
      model: {
        source: ''
      }
    };
  },
  methods: {
    // 自动去除空格和换行
    handleInputChange(e) {
      this.model.source = e.target.value.replace(/\s+/g, '').replace(/\n/g, '');
    },
    edit() {
      this.visible = true
      // this.$nextTick(() => {
      //   this.$refs.inputRef.focus();
      // });
    },
    handleOk(e) {
        // var reg = /[~`!#$%\^&*()_+\-=\[\]{};':"\\|,<>\/?]/
        // if (reg.test(this.model.source)) {
        //   this.$message.warning('禁止输入特殊字符！')
        //   return
        // }
        this.confirmLoading = true;
        var url = ''
        var params = ''
        if (this.action == 'rename') {
          url = '/api/erp/oa/fileManager/rename'
          params = `source=${this.target}&action=${this.action}&target=${encodeURIComponent(this.model.source)}`
        } else {
          url= '/api/erp/oa/fileManager/create'
          params = `id=${new Date().getTime()}&target=${this.target}&action=${this.action}&source=${encodeURIComponent(this.model.source)}`
        }
        postAction(url, params).then(res => {
          if (res.success) {
            this.$message.success(res.message)
            this.$emit('createOk')
          } else {
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.handleCancel()
        })
    },
    handleCancel(e) {
      this.model.source = ''
      this.confirmLoading = false
      this.visible = false;
    },
  },
};
</script>